/**
 * Created by Jackie on 2016/11/10.
 */


function Model() {
    this.mask = $('.wt_mask')
    this.confirm = $('.wt_confirm')
    this.inputs = {
        name: $('.name'),
        phone: $('.phone'),
        account: $('.account')
    }
    this.content = $('.wt_confirm_ctn_inner')
    this.alertContent = $('.wt_confirm_ctn_inner_alert')
    this.btns = $('.wt_confirm_btns')
}

$.extend(Model.prototype, {
    open: function (type, text) {
        this.mask.show()
        this.confirm.show()
        if (type == 'alert') {
            this.alertContent.show()
            this.alertContent.html(text)
            this.btns.hide()
            this.content.hide()
        } else {
            this.btns.show()
            this.content.show()
            this.alertContent.hide()
        }
    },
    close: function () {
        this.mask.hide()
        this.confirm.hide()
    },
    getValue: function (name) {
        return this.inputs[name].val()
    },
    clear: function () {
        this.input.val('')
    }

})

function Activity() {
    this.tpl_alert = '恭喜您！抢兑成功<br /><br />用户名：${nickname}<br /><br />商品名称：${prize} <a style="font-size:22px;border-left:0px;border-top:0px;border-right:0px;border-bottom:1px;color:#ed304e;white-space: normal;word-break: break-all;word-wrap: break-word;">${coupon}</a><br />中奖时间：${time}<br />使用网站：<a href="${link}" style="font-size:22px;border-left:0px;border-top:0px;border-right:0px;border-bottom:1px;white-space: normal;word-break: break-all;word-wrap: break-word;">${link}</a>。<br /><br />如在兑换过程中出现任何问题，请及时联系胖吴货栈，客服热线：400-077-8100，谢谢！[浦发银行上海分行]<br />'
    
    
    /*
     * 恭喜您！抢兑成功，用户姓名：111，商品名称：三星Galley TZS16U9H53-N86P5，使用网站：。若兑换过程中由于支付操作失误出现折扣码失效情况，可于24小时之后再次登陆兑换。如在兑换过程中出现任何问题，请及时联系旅游族，客服热线：400-801-3388,谢谢！[浦发银行上海分行]
     * */
}
$.extend(Activity.prototype, {
    saveStorage: function (obj) {
        var storage = localStorage
        storage.setItem("saveJson", JSON.stringify(obj));
    },
    init: function () {
        var self = this
        var json = localStorage.getItem("saveJson");
        self.params = null
        if (json != null)
            self.params = JSON.parse(json);

        self.model = new Model()
        self.bindEvents()
        // self.model.open()
        //列表
        self.render()

    },
    detailInit: function () {
        var self = this
        var json = localStorage.getItem("saveJson");
        self.params = null
        if (json != null)
            self.params = JSON.parse(json);

        self.model = new Model()

        var parser = new Poly9.URLParser(location.href)
        var data = {
            activity_id: parser.getQuery('activity_id')
        }
        this.services.info(data, function (res) {
            if (res.err_code == 200) {
                $('.JContent').html(res.err_msg.description)
                self.current_data = res.err_msg
            }
        })
        this.bindEvents()

    },
    render: function () {

        var self = this

        self.tpl_list = $('.tpl_list').html()

        self.services.list(function (res) {

            if (res.err_code == 200) {
                var prize = res.err_msg.prize
                self.is_bind = res.err_msg.is_bind

                var html = _.template(self.tpl_list)({prize: prize})
                self.prize = prize
                $('.goods').html(html)
            }
        })
    },
    sub: function (e, t) {
        return e.replace(/\${(.*?)}/gim, function (e, a) {
            return "string" == typeof t ? t : "undefined" == typeof t[a] ? "" : t[a]
        })
    },
    doBuy: function (params) {
        var self = this

        self.services.buy(params, function (res) {
            $("input").blur()
            if (res.err_code == 200) {
                self.saveStorage(params)
                self.params = params
                // alert('抢购成功')
                var alert_text = self.sub(self.tpl_alert, res)
                self.model.open('alert', alert_text)
            } else {
                self.params=null
                // alert(res.err_msg)
                self.model.open('alert', res.err_msg)
            }
        })

    },
    bindEvents: function () {
        var self = this

        //点抢购按钮
        $(document).on('touchstart', '.j-btn', function () {

            // self.current_data = self.prize[$this.attr('index')]
            if (self.params && self.params.account!=null && self.params.account.length>=15 && self.params.account.length<=16) {
                
                self.params.activity_id=self.current_data.id
                self.params.prize_id = self.current_data.prize_id
                self.doBuy(self.params)
            } else {
                self.model.open()
            }
        })

        //点弹窗ok按钮去抢购
        $(document).on('touchstart', '.j-ok', function () {
            var phone = self.model.getValue('phone')
            var account = self.model.getValue('account')
                       

            if(account.length!=15 && account.length!=16){
                self.model.open('alert', "请输入15位或者16位银行卡号")
                return
            
            }
            var name = self.model.getValue('name')
            var params = {
                name: name,
                phone: phone,
                account: account,
                activity_id: self.current_data.id
            }

            self.doBuy(params)

        })
        //关model
        $(document).on('touchstart', '.j-close', function () {
            self.model.close()
        })
    },
    services: {
         //环境变更
        REMOTE: 'http://119.29.169.29/pufa/api.php',
        list: function (cb) {
            //列表
            return $.ajax({
                url: this.REMOTE + '/RushBuy/index',
                type: 'POST',
                success: cb
            })
        },
        buy: function (data, cb) {
            //抢购
            return $.ajax({
                url: this.REMOTE + '/RushBuy/buy/',
                type: 'POST',
                data: data,
                success: cb
            })
        },
        info: function (data, cb) {
            //抢购
            return $.ajax({
                url: this.REMOTE + '/RushBuy/info',
                type: 'POST',
                data: data,
                success: cb
            })
        }
    }
})

_.templateSettings = {
    evaluate: /\{\{(.+?)\}\}/g,
    interpolate: /\{\{=(.+?)\}\}/g,
    escape: /\{\{-(.+?)\}\}/g
};

var myAct = new Activity()
